package com.wushi.lenovo.asia5b.app_widget.bottomtab;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.mvp.base.BaseAppCompatActivity;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.OrderRepository;
import com.wushi.lenovo.asia5b.app_config.ShopRepository;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_config.WishRepository;
import com.wushi.lenovo.asia5b.app_model.other.MainTabType;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.ChatDialog;

import timber.log.Timber;

import static java.lang.String.format;
import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/4/26.
 * 这里面存在业务耦合,目的不是为了抽取成控件,所以与MainActivity深度绑定
 */
public class WAlphaTabsIndicator extends LinearLayout implements View.OnClickListener {

    private WAlphaTabView tab_main_home;
    private WAlphaTabView tab_main_wish;
    private WAlphaTabView tab_main_shop;
    private WAlphaTabView tab_main_order;
    private WAlphaTabView tab_main_me;
    private WAlphaPiView tab_main_pi;
    private ViewPager viewPager;

    public WAlphaTabsIndicator(Context context) {
        super(context);
        init();
    }

    public WAlphaTabsIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WAlphaTabsIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_alphatabindicator, this, true);
        tab_main_home = findViewById(R.id.tab_main_home);
        tab_main_wish = findViewById(R.id.tab_main_wish);
        tab_main_shop = findViewById(R.id.tab_main_shop);
        tab_main_order = findViewById(R.id.tab_main_order);
        tab_main_me = findViewById(R.id.tab_main_me);
        tab_main_pi = findViewById(R.id.tab_main_pi);
        tab_main_home.setOnClickListener(this);
        tab_main_wish.setOnClickListener(this);
        tab_main_shop.setOnClickListener(this);
        tab_main_order.setOnClickListener(this);
        tab_main_me.setOnClickListener(this);
        tab_main_pi.setOnClickListener(this);
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        setCheckTabItem(tab_main_home);
    }

    /**
     * 设置右上角提醒数字
     *
     * @param type tab 类型
     */
    public void setNumber(MainTabType type) {
        if (type != null) {
            if (type == MainTabType.home) {
                tab_main_home.setNumber(0);
            } else if (type == MainTabType.wish) {
                tab_main_wish.setNumber(WishRepository.getWishCount());
            } else if (type == MainTabType.shop) {
                tab_main_shop.setNumber(ShopRepository.getShopCount());
            } else if (type == MainTabType.order) {
                tab_main_order.setNumber(OrderRepository.getOrderCount());
            } else if (type == MainTabType.me) {
                tab_main_me.setNumber(0);
            }
        }
    }

    /**
     * 切换底部选中样式
     *
     * @param v 对应tab控件
     *          所有的选中都必须走到这里
     */
    private void setCheckTabItem(View v) {
        tab_main_home.setSelected(v.getId() == R.id.tab_main_home);
        tab_main_wish.setSelected(v.getId() == R.id.tab_main_wish);
        tab_main_shop.setSelected(v.getId() == R.id.tab_main_shop);
        tab_main_pi.setSelected(v.getId() == R.id.tab_main_shop);
        tab_main_order.setSelected(v.getId() == R.id.tab_main_order);
        tab_main_me.setSelected(v.getId() == R.id.tab_main_me);
        int index = 0;
        if (v.getId() == R.id.tab_main_home) {
            index = 0;
        } else if (v.getId() == R.id.tab_main_wish) {
            index = 1;
        } else if (v.getId() == R.id.tab_main_shop) {
            index = 2;
        } else if (v.getId() == R.id.tab_main_order) {
            index = 3;
        } else if (v.getId() == R.id.tab_main_me) {
            index = 4;
        }
        if (getContext() instanceof BaseAppCompatActivity) {
            ((BaseAppCompatActivity) getContext()).checkTabItem(index);
        }
//        if (tab_main_home.isSelected() || tab_main_shop.isSelected()) {
//            mainActivity.drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
//        } else {
//            mainActivity.drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
//        }
//        if (tab_main_me.isSelected()) {
//            mainActivity.statusview.setVisibility(GONE);
//        } else {
//            mainActivity.statusview.setVisibility(VISIBLE);
//        }
    }

    /**
     * 切换底部选中样式根据角标
     *
     * @param position
     */
    public void setCheckTabItem(int position) {
        if (position == 1) {
            setCheckTabItem(tab_main_wish);
        } else if (position == 2) {
            setCheckTabItem(tab_main_shop);
        } else if (position == 3) {
            setCheckTabItem(tab_main_order);
        } else if (position == 4) {
            setCheckTabItem(tab_main_me);
        } else {//position == 0
            setCheckTabItem(tab_main_home);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /**
             * 首页,可以不登录
             */
            case R.id.tab_main_home:
                if (!v.isSelected()) {
                    setTabCurrenItem(0);
                }
                break;
            /**
             * 收藏,必须登录
             */
            case R.id.tab_main_wish:
//                if (isLoginAndChecked(v)) {
//                    setTabCurrenItem(1);
//                }
                new ChatDialog(getContext()).show();
                break;
            /**
             * 购物车/批发,必须登录
             */
            case R.id.tab_main_pi:
            case R.id.tab_main_shop:
                if (isLoginAndChecked(v)) {
                    setTabCurrenItem(2);
                }
                break;
            /**
             * 订单,必须登录
             */
            case R.id.tab_main_order:
                if (isLoginAndChecked(v)) {
                    setTabCurrenItem(3);
                }
                break;
            /**
             * 个人,必须登录
             */
            case R.id.tab_main_me:
                if (isLoginAndChecked(v)) {
                    setTabCurrenItem(4);
                }
                break;
            default:
                break;

        }
    }

    /**
     * 必须登录切不被选中
     *
     * @param v
     * @return 必须登录切不被选中->true
     */
    private boolean isLoginAndChecked(View v) {
        if (UserRepository.isLogin(getContext())) {
            if (!v.isSelected()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 切换底部选中样式根据角标
     *
     * @param i
     */
    public void setTabCurrenItem(int i) {
        try {
            int count = viewPager.getAdapter().getCount();
            d(format("--->>>>>>>>---viewPager.getAdapter().getCount():%d", count));
            if (i < count) {
                viewPager.setCurrentItem(i, false);
                setCheckTabItem(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 检测选中是否一一对应
     * 防止出现fragment≠tab
     *
     * @return
     */
    public void checkItem() {
        int currentItem = viewPager.getCurrentItem();
        d(format("--->>>>>>>>---当前选中:%d", currentItem));
        boolean checkItem = true;
        switch (currentItem) {
            case 0:
                if (!tab_main_home.isSelected()) {
                    checkItem = false;
                    Timber.d(format("--->>>>>>>>---不对应:tab_main_home"));
                }
                break;
            case 1:
                if (!tab_main_wish.isSelected()) {
                    checkItem = false;
                    Timber.d(format("--->>>>>>>>---不对应:tab_main_wish"));
                }
                break;
            case 2:
                if (!tab_main_shop.isSelected()) {
                    checkItem = false;
                    Timber.d(format("--->>>>>>>>---不对应:tab_main_shop"));
                }
                break;
            case 3:
                if (!tab_main_order.isSelected()) {
                    checkItem = false;
                    Timber.d(format("--->>>>>>>>---不对应:tab_main_order"));
                }
                break;
            case 4:
                if (!tab_main_me.isSelected()) {
                    checkItem = false;
                    Timber.d(format("--->>>>>>>>---不对应:tab_main_me"));
                }
                break;
        }
        if (!checkItem) {
            setTabCurrenItem(0);
        }

        //其他页面是必须登录,所以未登录就必须重置到第一个页面
        if (!UserRepository.isLogin() && viewPager.getCurrentItem() != 0) {
            setTabCurrenItem(0);
        }
    }
}
